<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-28
 * Time: 11:17
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\model\Product as ProductModel;
use app\api\model\User as UserModel;
use app\api\model\TeamUser as TeamUserModel;
use app\api\model\StoreUser as StoreUserModel;
use app\api\model\Team as TeamModel;
use app\api\model\ProductOrder as ProductOrderModel;

use app\api\service\Token as TokenService;
use app\lib\exception\ParameterException;

class Product extends BaseController
{
    protected $beforeActionList = [
        'checkPrimaryScope' => ['only' => 'getDataList']
    ];

    // 获取产品列表
    public function getDataList()
    {
        $uid = TokenService::getCurrentUid();
        $userInfo = UserModel::get($uid);
        $where[] = ['up_down','eq',1];
        $where[] = ['product_type','eq',1];
        if($userInfo['type'] == 1) {
            if($userInfo['store_id']) {
                $where[] = ['store_id','eq',$userInfo['store_id']];
            }
        } elseif($userInfo['type'] == 2 || $userInfo['type'] == 3  || $userInfo['type'] == 4 || $userInfo['type'] == 8) {
                $teamUser = StoreUserModel::where('user_id',$userInfo['id'])->find();
                $where[] = ['store_id','eq',$teamUser['store_id']];
        }
        $time = time();
        $where[] = ['end_time','egt',$time];
        return ProductModel::where($where)->with('img,store')
            ->order('order_sort desc')
            ->select()->each(function ($item) use($time){
                $info = TokenService::getProductPrice($item);
                $item['price'] = $info['price'];
                $item['min_time'] = ($info['end_time'] - $time)*1000;
            })->toArray();
    }


    // 获取产品详情
    public function getDataInfo($id)
    {
        $info = ProductModel::with('img,store')->where('id',$id)->find();
        if($info['product_type'] == 1) {
            $info['end_time_one'] = $info['end_time'];
            $info['now_time'] = time();
            $info['end_time'] = date('Y-m-d H:i:s',$info['end_time']);
        } else {
            $info['end_time_one'] = $info['end_time'];
            $info['now_time'] = time();
            $info['end_time'] = date('Y-m-d H:i:s',time()+$info['end_time']*86400);
        }
        return TokenService::getProductPrice($info);
    }


    // 获取团员专属卡
    public function getProductTypeInfo($scene)
    {
        $scene = explode("-",$scene);
        $uid = TokenService::getCurrentUid();
        $userInfo = UserModel::get($uid);
        if($userInfo['type'] != 1) {
            throw new ParameterException([
                'msg' => [
                    'product' => '不可购买专属卡'
                ]
            ]);
        }
        $teamUserInfo = UserModel::where('id',$scene[1])->find();
        if($teamUserInfo['type'] == 5 || $teamUserInfo['type'] == 6) {
            $teamUser = TeamUserModel::where('user_id',$teamUserInfo['id'])->find();
            $productOrder = ProductOrderModel::where('order_type','eq',2)
                ->where('order_team_user_id','eq',$teamUser['id'])
                ->where('status','gt',1)
                ->find();
            if($productOrder) {
                throw new ParameterException([
                    'msg' => [
                        'product' => '该专属卡已经被购买'
                    ]
                ]);
            }
            $team = TeamModel::where('id',$teamUser['team_id'])->find();
            $product = ProductModel::with('img')->where('id',$team['product_id'])->find();
            if(!$product) {
                throw new ParameterException([
                    'msg' => [
                        'product' => '专属卡为空'
                    ]
                ]);
            }
            $product['team_user_id'] = $scene[1];
            return $product;
        } else {
            throw new ParameterException([
                'msg' => [
                    'product' => '别的用户类型没有专属卡'
                ]
            ]);
        }
    }

    public function setProjectCode($project_id,$userid){
        if($userid==null)
            $userid = TokenService::getCurrentUid();
        $data['scene'] = 'user-'.$userid.'pid='.$project_id;
        $data['width'] = 280;
        $data['page'] = 'pages/product/product';
        $name = 'user-'.$userid.'pid-'.$project_id;
        $token = new TokenService();
        $token->setCodePub($data,$name);
        return [
            'url' => config('setting.invite_img_prefix').'/'.$name.'.png'
        ];
    }

    // 自动下架
    public function setProductUpDown()
    {
        $time = time();
        $where[] = ['end_time','lt',$time];
        $where[] = ['up_down','eq',1];
        ProductModel::where($where)->update([
            'up_down' => 2
        ]);
    }
}